/*

	Zheng Yao

	M334J523

	Program #2

	

Function: 	struct, createNode

		  This program ask the user for a list of book title. By using linked

		  list, this program will able to store the list of book title inside 

		  dynamic memory and link one to another using it. In the end, this 

		  program will print the list of book title out from the note by using

		  linked list again.

Inputs	:	None (entered by user)

Outputs	:	List of title have been entered.

Return	: 0



Psuedo Code:





*/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



typedef struct node

{

          void  *dataPtr;

   struct node  *link;

} NODE;

// Declaring the node structure which consist of data and a link.



NODE *createNode (void *itemPtr)

{

   NODE *nodePtr;

   nodePtr  = (NODE*) malloc (sizeof (NODE));

   nodePtr -> dataPtr =  itemPtr;

   nodePtr -> link    =  NULL;

   return  nodePtr;

}

// A function that will pass in the address of the data.

// The function also moves the nodePtr from the data section to link section.

// Then it return NULL in the link and return the address



int main (void)

{

   	char *newData;

    char *nodeData;

    char *titlePtr;

	char check [3] = "end";

    int flag = 1;

	NODE *node, *head;

		

//First input :

    do 

	{			

		newData  = (char *) malloc (sizeof (char));

		titlePtr = (char *) malloc (sizeof (char));

		//declaring a dynamic memory to store the inputs.

		

		printf("Enter book title and to stop press end \n ");

		gets(titlePtr);

		// Prompt to key in the book title.

		

		if(strcmp(titlePtr,check) != 0) 

        // Compare and check if the title enter is end. If it is true, then 

        // return 0 and points to else.

		{

			strcpy(newData, titlePtr);

			node = createNode (newData);

			flag = 0;

			// coppy the strings to newData and send it to createNode function.

		}		

		else

			flag = 1;

			// To let the do while loop keep repeating so to prevent the first

			// entries is end.

		

	}while(flag != 0);	

	

	flag = 1;

	// Reset back the checker.



//Other inputs :

	

    do 

	{			

		newData  = (char *) malloc (sizeof (char));

		titlePtr = (char *) malloc (sizeof (char));

		//declaring a dynamic memory to store the inputs.

		

		printf("Enter book title and to stop press end \n ");

		gets(titlePtr);

		// Prompt to key in the book title.

		

		if(strcmp(titlePtr,check) != 0) 

		// Compare and check if the title enter is end. If it is true, then 

        // return 0 and end the do while loop.

		{

			strcpy(newData, titlePtr);

//			node = createNode (newData);

			node -> link = createNode (newData);

		}		

		else

			flag = 0;

			// To exit the loop. 

			

	}while(flag != 0);



		

	head = node;



/*

    // Let's transverse the list

	int x = 0;

	while (node != NULL)

	{

		x++;

		nodeData = (char*)node->dataPtr;

		printf ("\nThe data of node %d is %s.", x, nodeData);

		getchar();

		//node = node->link;

	}

//*/  



	nodeData = (char*)node->dataPtr;

	printf("Data from node 1 : %s\n", nodeData);

	nodeData = (char*)node->link->dataPtr;

	printf("Data from node 2 : %s\n", nodeData);

	nodeData = (char*)node->link->dataPtr;

	printf("Data from node 3 : %s\n", nodeData);

	nodeData = (char*)node->link->dataPtr;

	printf("Data from node 4 : %s\n", nodeData);	

	

//* /



   system ("PAUSE");

   return 0;

}
